#include "inboxListData.h"
inboxListData::inboxListData(QString xmlFileName)
{
	xmlFile = xmlFileName;
}

void inboxListData::saveinboxXml()
{

	cout<<"in saveinboxXml"<<endl;
	//QFile file("inbox.xml");
	QFile file(xmlFile);
	if(file.exists())//如果文件存在则先删除后创建保存
		file.remove();
	if((mInboxList.count() == 0))
	{
		file.close();
		return;//如果列表为空，则不需要保存
	}
	if(!file.open(QIODevice::ReadWrite))
		return;
	QTextStream out(&file);
	out.setCodec("UTF-8");
	QDomDocument doc;

	QDomProcessingInstruction instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
	/*写入头*/
	doc.appendChild(instruction);
	/*写入根结点*/
	QDomElement root = doc.createElement("inboxs");
	doc.appendChild(root);

	for(int i=0; i<mInboxList.count(); i++)
	{
		/*写入people结点*/
		QString tag = QString("people%1").arg(i);
		QDomElement peopleTag = doc.createElement(tag);
		root.appendChild(peopleTag);

	
		QDomElement phoneNumTag = doc.createElement("phonenum");
		peopleTag.appendChild(phoneNumTag);

		QDomText phoneNum_txt = doc.createTextNode(mInboxList[i].phoneNum);
		phoneNumTag.appendChild(phoneNum_txt);

		for(int j=0; j<mInboxList[i].mMessageList.count(); j++)
		{

			/*写入姓名结点*/
			QString tag = QString("message%1").arg(j);
			QDomElement message = doc.createElement(tag);
			peopleTag.appendChild(message);


			QDomElement type = doc.createElement("type");
			message.appendChild(type);
			QDomText type_txt = doc.createTextNode(mInboxList[i].mMessageList[j].type);
			type.appendChild(type_txt);

			QDomElement content = doc.createElement("content");
			message.appendChild(content);
			QDomText content_txt = doc.createTextNode(mInboxList[i].mMessageList[j].content);
			content.appendChild(content_txt);
		}
	}
	doc.save(out,4,QDomNode::EncodingFromTextStream);//将准备好的xml文档写入文件中

	file.close();
}

void inboxListData::loadinboxXml()
{
	qDebug("locad inbox from inbox.xml");
	//QFile file("inbox.xml");
	QFile file(xmlFile);
	if(!file.exists()) return;//如果文件不存在，则不需要加载
	if(!file.open(QIODevice::ReadWrite))
	{
		qDebug("error while open inbox.xml");
	}
	QDomDocument doc;
	QString errorStr;
	int errorLine;
	int errorColumn;
	if(!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
	{
		cout<<"error setContent"<<endl;
	}
	//QString people,name,phoneNum,black;
	mInboxList.clear();
	QDomNodeList list = doc.childNodes();
	for(int i=0; i<list.count();i++){
		QDomNodeList list_2 = list.at(i).childNodes();//people%d
		for(int j=0; j<list_2.count(); j++)
		{
			QDomNodeList list_3 = list_2.at(j).childNodes();

			inboxData tmpInboxData;
			for(int k=0; k<list_3.count(); k++)
			{	
				if(k == 0)//phoneNum
				{
					tmpInboxData.phoneNum = list_3.at(0).toElement().text();
					continue;
				}

				smsData tmpSmsData;
				QDomNodeList list_4 = list_3.at(k).childNodes();

				tmpSmsData.type = list_4.at(0).toElement().text();
				tmpSmsData.content = list_4.at(1).toElement().text();
				tmpInboxData.mMessageList.push_back(tmpSmsData);
			}
			mInboxList.push_back(tmpInboxData);
		}
	}
	file.close();
}

void inboxListData::setDataChanged()
{
	emit dataChanged();
}

int inboxListData::findPhoneNum(QString &str)
{
	int result = -1;
	for(int i =0; i< mInboxList.count(); i++)
	{
		if(mInboxList[i].phoneNum == str)
			return i;
	}
	return result;
}
void inboxListData::insertMessage(QString &phoneNum, smsData &data)
{
	//1.查找有无当前联系人
	int tmpindex = findPhoneNum(phoneNum);
	//2.如果有则添加短信至联系人
	if(tmpindex != -1)
	{
		mInboxList[tmpindex].mMessageList.push_back(data);	
	}
	else{//2.1如果没有则添加一个联系人再添加短信
		inboxData tmpInboxData;
		tmpInboxData.phoneNum = phoneNum;
		tmpInboxData.mMessageList.push_back(data);
		mInboxList.push_back(tmpInboxData);	
	}
	emit dataChanged();
}
bool inboxListData::delMessage(QString &phoneNum, int index)
{
	//1.查找有无当前联系人
	bool ans = false;
	int tmpindex = findPhoneNum(phoneNum);
	mInboxList[tmpindex].mMessageList.removeAt(index);
	if(mInboxList[tmpindex].mMessageList.count() == 0)
	{
		mInboxList.removeAt(tmpindex);
		ans = true;
	}
	emit dataChanged();
	return ans;
}




